<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="roleDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="学号">
							<el-input v-model="state.ruleForm.Id" placeholder="请输入学号" clearable disabled />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="学生姓名">
							<el-input v-model="state.ruleForm.name" placeholder="请输入学生姓名" clearable disabled />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="学习态度">
							<el-input v-model="state.ruleForm.b1" placeholder="请对该同学学习态度进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="使用规范">
							<el-input v-model="state.ruleForm.b2" placeholder="请对该同学设备使用规范进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="操作能力">
							<el-input v-model="state.ruleForm.b3" placeholder="请对该同学独立操作能力进行评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="分工协作">
							<el-input v-model="state.ruleForm.b4" placeholder="请对该同学分工协作能力进行评分" clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="gradeEvaluationDialog">
import { reactive, ref } from 'vue';
import { storeToRefs } from 'pinia';
import { useUserInfo } from '/@/stores/userInfo';
const stores = useUserInfo();
const { userInfos } = storeToRefs(stores);
import { getStudentEvaluationInfo, updateStudentGrade } from '/@/api/grade';
import { ElMessage } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const roleDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		Id: '',
		name: '',
		b1: 0,
		b2: 0,
		b3: 0,
		b4: 0,
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
		experiment_id: '',
	},
});

// 打开弹窗
const openDialog = (row: any, id: any) => {
	state.ruleForm = row;
	state.dialog.title = '小组互评';
	state.dialog.submitTxt = '完 成';
	state.dialog.isShowDialog = true;
	state.dialog.experiment_id = id;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	getStudentEvaluationInfo({ experiment_id: state.dialog.experiment_id, evaluatee_id: state.ruleForm.Id, evaluator_id: userInfos.value.Id })
		.then((res) => {
			if (res.code === '2000') {
				const update_info = {
					evaluated_id: res.data[0].evaluated_id,
					info: {
						evaluated_grade: state.ruleForm.b1 * 0.12 + state.ruleForm.b2 * 0.05 + state.ruleForm.b3 * 0.06 + state.ruleForm.b4 * 0.03,
					},
				};
				updateStudentGrade(update_info)
					.then((result) => {
						if (result.code === '2000') {
							ElMessage.success('提交小组互评成功!');
							closeDialog();
							emit('refresh');
						} else {
							ElMessage.error('提交小组互评失败!');
						}
					})
					.catch((err) => {
						ElMessage.error('提交小组互评失败!' + err);
					});
			} else {
				ElMessage.error('获取学生互评信息失败!');
			}
		})
		.catch((err) => {
			ElMessage.error('获取学生互评信息失败!' + err);
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
